package com.zlf.service;

import com.zlf.feign.WalletFeignClient;
import com.zlf.mapper.BusinessMapper;
import com.zlf.po.Business;
import com.zlf.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Qualifier("com.zlf.feign.WalletFeignClient")
    @Autowired
    private WalletFeignClient walletFeignClient;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) {
        return businessMapper.listBusinessByOrderTypeId(orderTypeId);
    }

    @Override
    public Business getBusinessById(Integer businessId) {
        return businessMapper.getBusinessById(businessId);
    }

    @Override
    public List<Business> listBusiness() {
        return businessMapper.listBusiness();
    }

    @Override
    public List<Business> selectBusiness(String key) {
        return businessMapper.selectBusiness(key);
    }

    @Override
    public Business login(int businessId, String pass) throws Exception {
        Business business=businessMapper.getBusinessById(businessId);
        if(business==null){
            throw new Exception("用户不存在");
        }
        if(pass.equals(business.getPass())){
            return business;
        }
        else{
            throw new Exception("密码错误");
        }
    }

    @Override
    public int saveBusiness(Business pram) throws Exception{
        Business business=businessMapper.getBusinessById(pram.getBusinessId());
        if(business!=null){
            throw new Exception("用户已注册");
        }
        walletFeignClient.createWallet(Long.valueOf(pram.getBusinessId()), pram.getPass());
        return businessMapper.saveBusiness(pram);
    }

    @Override
    public int updateBusiness(Business business) {
        Business business1=businessMapper.getBusinessById(business.getBusinessId());
        if(business.getBusinessName()!=null){
            business1.setBusinessName(business.getBusinessName());
        }
        if(business.getBusinessAddress()!=null){
            business1.setBusinessAddress(business.getBusinessAddress());
        }
        if(business.getBusinessExplain()!=null){
            business1.setBusinessExplain(business.getBusinessExplain());
        }
        if(!Double.isNaN(business.getStarPrice())){
            business1.setStarPrice(business.getStarPrice());
        }
        if(!Double.isNaN(business.getDeliveryPrice())){
            business1.setDeliveryPrice(business.getDeliveryPrice());
        }
        return businessMapper.updateBusiness(business1);
    }
}
